package com.example.javaow.controller;/*
 * @Auther:Sadie
 * @Date:2025/8/12
 * @Description:
 * @VERSON:1.8
 */

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.javaow.domain.base.PageResult;
import com.example.javaow.domain.base.ResponseResult;
import com.example.javaow.domain.dto.DeviceDto;
import com.example.javaow.domain.vo.DeviceVo;
import com.example.javaow.service.DeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/device")
@RequiredArgsConstructor
@Slf4j
public class DeviceController {
    @Autowired
    private DeviceService deviceService;

    @PostMapping("/add")
    public ResponseResult add(@RequestBody DeviceDto deviceDto) {
        log.info("添加设备：{}", deviceDto);
        return deviceService.add(deviceDto);
    }

    @PutMapping("/update")
    public ResponseResult update(@RequestBody DeviceDto deviceDto) {
        log.info("更新设备：{}", deviceDto);
        return deviceService.update(deviceDto);
    }

    @PostMapping ("/delete")
    public ResponseResult delete(@RequestBody Long[] ids) {
        log.info("删除设备：{}", ids);
        return deviceService.delete(ids);
    }

    @GetMapping("/getById")
    public ResponseResult getById(@Param("id") Long id) {
        log.info("查询设备：{}", id);
        return deviceService.getById(id);
    }

    @GetMapping("/list")
    public ResponseResult<PageResult<DeviceVo>> getDevicePage(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            @RequestParam(value = "deviceAddress", required = false) String deviceAddress,
            @RequestParam(value = "deviceNumber", required = false) String deviceNumber,
            @RequestParam(value = "deviceName", required = false) String deviceName,
            @RequestParam(value = "deviceType", required = false) String deviceType,
            @RequestParam(value = "groupId", required = false) Long groupId,
            @RequestParam(value = "status", required = false) String status,
            @RequestParam(value = "location", required = false) String location,
            @RequestParam(value = "keyword", required = false) String keyword) {
        log.info("分页查询设备列表，页码：{}，每页大小：{}，设备地址：{}，设备编号：{}，设备名称：{}，设备类型：{}，分组ID：{}，状态：{}，位置：{}，关键字：{}",
                pageNum, pageSize, deviceAddress, deviceNumber, deviceName, deviceType, groupId, status, location, keyword);

        IPage<DeviceVo> page = deviceService.getByConditions(
                pageNum, pageSize,
                deviceAddress, deviceNumber, deviceName,
                deviceType, groupId, status, location, keyword
        );

        PageResult<DeviceVo> result = new PageResult<>();
        result.setTotal(page.getTotal());
        result.setPages((int) page.getPages());
        result.setCurrent((int) page.getCurrent());
        result.setSize((int) page.getSize());
        result.setRecords(page.getRecords());

        return ResponseResult.success("查询成功", result);
    }

}
